local base = _G
module("GUI.EventHandler.Form.Stock")

local os = base.os.getenv("OS")
if os == "Windows_NT" then
	rootdir = ".\\"
else
	rootdir = "\\Program Files\\wmssystem\\"
end

base.print("DBG:Eventhandler GUI.EventHandler.Form.Stock Loaded.\n")
function Parse(methon,para)
	local utils = base.require("Library.Utils.Utils")
	para = utils.Base64_Decode(para)
	base.print("DBG: methon: " .. methon .. " para: " .. para .. "\n")
	if(methon == "back") then
		back()
	elseif(methon == "Query") then
		query(para)
	else
		base.print("DBG:Unknown action\n")
	end
	base.os.exit(0)
end

function back()
	local file = base.io.open(rootdir .. "\\tmp\\form.txt","w")
	file:write("main")
	file:close()
end

function query(para)
	local f = base.require("Function.stock_management")
	local content = f.Query2(para)
	local utils = base.require("Library.Utils.Utils")
	
	if content["SN"] == nil then
		utils.MessageBox("No such stock!","Error",1)
		return 0
	end
	
	local h_xmlfile = base.io.open(rootdir .. "XML\\stock_detail.xml","rb")
	xmlfile = h_xmlfile:read("*all")
	h_xmlfile:close()
	
	local stockdetail = "Stock Detail:\nSN:" .. (content["SN"] or "Unavailable") .. "\nName:" .. (content["Name"] or "Unavailable") .. "\nType:" .. (content["TypeID"] or "Unavailable") .. "\nWeight:" .. (content["Weight"] or "Unavailable") .. "\nLocation:" .. (content["Location"] or "Unavailable") .. "\nUnit:" .. (content["Unit"] or "Unavailable") .. "\n"
	xmlfile = base.string.gsub(xmlfile,"{StockDetail}",stockdetail);
	
	local h_xmlfile = base.io.open(rootdir .. "XML\\tmp\\stock_detail.xml","wb")
	h_xmlfile:write(xmlfile)
	h_xmlfile:close()

	local file = base.io.open(rootdir .. "\\tmp\\form.txt","w")
	file:write("stock_detail")
	file:close()
end